package com.jet.core.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author dcl
 * @since 2024/12/7 22:06
 */
public class MyBatisGenerator {

    private static final String URL = "jdbc:postgresql://127.0.0.1:5432/postgres?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=utf-8";
    private static final String USERNAME = "postgres";
    private static final String PASSWORD = "123456";

    // 子模块包名
    private static final String SUB_MODULE = "shop-goods\\";
    // 子模块下父包模块名
    private static final String MODULE_NAME = "goods";

    private static final List<String> includeTableNames = new ArrayList<>();

    static {

        includeTableNames.add("m_goods");
        includeTableNames.add("m_goods_category");
        includeTableNames.add("m_goods_sku");
        includeTableNames.add("m_goods_sku_spec");
        includeTableNames.add("m_goods_sku_tag");
        includeTableNames.add("m_goods_spec");
        includeTableNames.add("m_goods_spu");
        includeTableNames.add("m_goods_spu_picture");
        includeTableNames.add("m_goods_tag");
        includeTableNames.add("m_goods_unit");
    }

    public static void main(String[] args) {
        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
//                .dataSourceConfig(builder -> builder
//                        .databaseQueryClass(SQLQuery.class)
//                        .typeConvert(new PostgreSqlTypeConvert())
//                        .dbQuery(new PostgreSqlQuery())
//                )
                .globalConfig(builder -> builder
                        .author("author")
                        .enableSwagger()
                        .outputDir(SUB_MODULE + "src\\main\\java") //生成目录 子模块+包名
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig(builder -> builder
                                .parent("com.jet")
                                .moduleName(MODULE_NAME) // 设置父包模块名
                                .entity("domain.entity")
                                .mapper("mapper")
                                .service("service")
                                .serviceImpl("service.impl")
//                        .xml("mappers")
                                .pathInfo(Collections.singletonMap(OutputFile.xml, SUB_MODULE + "src\\main\\resources\\mapper"))
                )
                .strategyConfig(builder -> builder
                                .addInclude(includeTableNames) // 设置需要生成的表名
                                .addTablePrefix("m_")
                                .entityBuilder()
                                .enableLombok()
//                                .enableTableFieldAnnotation() // 设置字段注释
                                .controllerBuilder()
                                .enableRestStyle() // 启用rest风格
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
